package com.mq.rocketmq.ordermsg;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xiaoqiang
 * @Description:
 * @Date: 2024/7/2
 * @Version: 1.0
 */
public class OrderMsgSender {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("example_group_name");

        producer.setNamesrvAddr("localhost:9876");

        String topic = "orderlyTopic";

        producer.start();

        //配置队列选择器，决定消息放在哪个队列中
        MessageQueueSelector messageQueueSelector = new MessageQueueSelector() {

            /**
             *
             * @param list  默认四个队列
             * @param message 消息本身
             * @param o 这里利用orderId来区分放入哪个队列中,所以这里的o就是orderId
             * @return
             */
            @Override
            public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
                int i = Integer.parseInt(o.toString());
                int index = (int) (i % list.size());
                return list.get(i);
            }
        };

        createOrders().forEach(order -> {
            Message message = new Message(topic, order.toString().getBytes());
            try {
                producer.send(message, messageQueueSelector, order.getOrderId());
            } catch (MQClientException e) {
                throw new RuntimeException(e);
            } catch (RemotingException e) {
                throw new RuntimeException(e);
            } catch (MQBrokerException e) {
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        producer.shutdown();
    }

    /**
     * 模拟多线程创建订单
     *
     * @return
     */
    private static List<Order> createOrders() {
        List<Order> objects = new ArrayList<>();

        Order order1 = new Order("0000001", "创建订单");
        Order order11 = new Order("0000003", "创建订单");
        Order order2 = new Order("0000001", "确认订单");
        Order order22 = new Order("0000003", "确认订单");
        Order order3 = new Order("0000001", "支付订单");
        Order order33 = new Order("0000003", "支付订单");
        Order order4 = new Order("0000001", "完成订单");
        Order order44 = new Order("0000003", "完成订单");

        objects.add(order1);
        objects.add(order11);
        objects.add(order2);
        objects.add(order22);
        objects.add(order3);
        objects.add(order33);
        objects.add(order4);
        objects.add(order44);
        return objects;

    }
}
